package com.xian.cloud.controller;

import com.xian.cloud.service.HttpAPIService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: xlr
 * @Date: Created in 下午10:35 2018/10/28
 */
@RestController
@RequestMapping("lcn")
public class UserController {


    @Autowired
    private JavaMailSender mailSender;


    //读取配置文件中的参数
    @Value("${spring.mail.username}")
    private String Sender;

    @Value( "${spring.boot.admin.notify.mail.to}" )
    private String to;

    @RequestMapping("start")
    public void startTime(){
        AtomicInteger ack = new AtomicInteger();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                ack.addAndGet( 1 );
                System.out.println("线程启动循环次数："+ack.get());
                String str = null;
                try {
                    str = httpAPIService.doGet("http://school.tengyue360.com/login");
                    if(StringUtils.isNotBlank( str ) && str.contains( "腾跃合作校" )){
                        System.out.println("服务正常");
//                        SimpleMailMessage message = new SimpleMailMessage();
//                        message.setFrom(Sender);
//                        String[] toSend = to.split( "," );
//                        message.setTo(toSend); //自己给自己发送邮件
//                        message.setSubject("主题：hello");
//                        message.setText("测试邮件内容 ++++++++++++++++ 服务正常  ");
//                        mailSender.send(message);
                    }else{
                        SimpleMailMessage message = new SimpleMailMessage();
                        message.setFrom(Sender);
                        String[] toSend = to.split( "," );
                        message.setTo(toSend); //自己给自己发送邮件
                        message.setSubject("主题：腾跃双师服务无法访问，赶紧起床备战");
                        message.setText("腾跃双师服务无法访问，此监控30秒访问一次");
                        mailSender.send(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    SimpleMailMessage message = new SimpleMailMessage();
                    message.setFrom(Sender);
                    String[] toSend = to.split( "," );
                    message.setTo(toSend); //自己给自己发送邮件
                    message.setSubject("主题：监听服务异常");
                    message.setText("监听服务异常");
                    mailSender.send(message);
                }
            }
        };

        Timer timer = new Timer();
        timer.schedule(timerTask,10,30000);
    }

    @Autowired
    private HttpAPIService httpAPIService;

    @RequestMapping("http")
    public String test() throws Exception {
        String str = httpAPIService.doGet("http://school.tengyue360.com/login");
        if(StringUtils.isNotBlank( str ) && str.contains( "腾跃合作校" )){
            return "服务正常";
        }
        return "hello";
    }


}
